package com.imooc.controller;

import com.imooc.pojo.MyConfig;
import com.imooc.pojo.Stu;
import com.imooc.pojo.Student;
import com.imooc.utils.JSONResult;
import com.imooc.utils.MyAsyncTest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;


@Controller
//@RestController
@Slf4j
public class HelloController {


    @RequestMapping("hello")
//  @ResponseBody
    public String hello() {
        return "Hello imooc!";
    }

    @Autowired
    private Stu stu;

    @GetMapping("getStu")
    public Object getStu() {
        return stu;
    }

    @Autowired
    private MyConfig myConfig;

    @Autowired
    private MyAsyncTest myAsyncTest;

    @GetMapping("getMyConfig")
    public Object getMyConfig() {
        myAsyncTest.publicMsg();
        log.info("跳过异步");
        return myConfig;
    }

    @Value("${self.custom.config.sdkSecret}")
    private String sdkSecret;
    @Value("${self.custom.config.host}")
    private String host;

    @GetMapping("getYmlCustomConfig")
    public Object getYmlCustomConfig() {
        return sdkSecret + ";\t" + host;
    }

    @GetMapping("getStudent")
    public JSONResult getStudent() {
        Student stu = new Student();
        stu.setName("imooc");
        stu.setAge(18);

//        log.info(stu.toString());
//        log.info(stu.toString());
//        log.warn(stu.toString());
//        log.error(stu.toString());

        return JSONResult.ok(stu);
    }

    @PostMapping("upload")
    public String upload(MultipartFile file) throws Exception {
        file.transferTo(new File("D:/test/" + file.getOriginalFilename()));

        return "上传成功";
    }
}
